/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import com.lmx.common.constants.CommonConstants;
import com.lmx.common.constants.ICommonConstants;
import com.lmx.common.util.CommonUtil;
import com.lmx.persistence.service.HealthInfoService;
import com.lmx.persistence.service.MessageService;
import com.lmx.repoDomain.exception.BusinessException;
import com.lmx.repoDomain.tmodel.Employee;
import com.lmx.repoDomain.tmodel.HbeaconWarning;
import io.katharsis.repository.annotations.JsonApiResourceRepository;
import io.katharsis.repository.annotations.JsonApiSave;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

import javax.annotation.PostConstruct;

@JsonApiResourceRepository(HbeaconWarning.class)
@Component
public class HbeaconWarningRepository extends BaseRepository<HbeaconWarning> {

	@Autowired
	private MessageService messgeService;

	@PostConstruct
	public void init() {
		dslJpaService.setEntityClass(HbeaconWarning.class);
	}

	@Override
	@JsonApiSave
	public HbeaconWarning save(HbeaconWarning record) {
		if (record.getId() == null) {
			//新建预警信息 若状态为已处理设置处理时间
			if(CommonConstants.DICTTYPE.BUTTON_STATUS_3.label().equals(record.getFlag().getTypecode())){
				if (record.getOperateTime() == null) {
					record.setOperateTime(CommonUtil.getCurrTimes());
				}
			}
			dslJpaService.create(record);
			if (CommonConstants.DICTTYPE.BUTTON_STATUS_1.label().equals(record.getFlag().getTypecode())) { // 发送消息   未处理
				messgeService.warningRemind(record,null);
			}
		} else {
			if (record.getDelStatus() == 1) {// 删除

			} else {
				HbeaconWarning hwData=(HbeaconWarning) dslJpaService.findById(record.getId());
				if(hwData!=null&&hwData.getFlag()!=null){
					// status已处理(人工)->已处理(人工)/已处理(已复位) 返回错误码
					if (CommonConstants.DICTTYPE.BUTTON_STATUS_3.label().equals(hwData.getFlag().getTypecode())
							&& (CommonConstants.DICTTYPE.BUTTON_STATUS_3.label().equals(record.getFlag().getTypecode())
									|| CommonConstants.DICTTYPE.BUTTON_STATUS_2.label().equals(record.getFlag().getTypecode()))) {
						throw new BusinessException(null, CommonConstants.ERROR_CODE.REPEAT_ERROR.label(), null);
					}
					// status已处理(已复位)->已处理(已复位) 返回错误码
					if (CommonConstants.DICTTYPE.BUTTON_STATUS_2.label().equals(hwData.getFlag().getTypecode())
							&& CommonConstants.DICTTYPE.BUTTON_STATUS_2.label().equals(record.getFlag().getTypecode())) {
						throw new BusinessException(null, CommonConstants.ERROR_CODE.REPEAT_ERROR.label(), null);
					}
					// status已处理(人工)/已处理(已复位)->未处理 返回错误码
					if ((CommonConstants.DICTTYPE.BUTTON_STATUS_3.label().equals(hwData.getFlag().getTypecode())
							|| CommonConstants.DICTTYPE.BUTTON_STATUS_2.label().equals(hwData.getFlag().getTypecode()))
							&& CommonConstants.DICTTYPE.BUTTON_STATUS_1.label().equals(record.getFlag().getTypecode())) {
						throw new BusinessException(null, CommonConstants.ERROR_CODE.REPEAT_ERROR.label(), null);
					}
					// status未处理/已处理(已复位)->已处理(人工)  设置处理时间
					if ((CommonConstants.DICTTYPE.BUTTON_STATUS_1.label().equals(hwData.getFlag().getTypecode())
							|| CommonConstants.DICTTYPE.BUTTON_STATUS_2.label().equals(hwData.getFlag().getTypecode()))
							&& CommonConstants.DICTTYPE.BUTTON_STATUS_3.label().equals(record.getFlag().getTypecode())) {
						if (record.getOperateTime() == null) {
							record.setOperateTime(CommonUtil.getCurrTimes());
						}
					}
				}
				if (record.getFlag() != null
						&& CommonConstants.DICTTYPE.BUTTON_STATUS_1.label().equals(record.getFlag().getTypecode())) { // 发送消息未处理
					messgeService.warningRemind(record, null);
				}
				
			}
			dslJpaService.update(record);
		}
		return record;
	}

}
